package bold.redisson;

import org.junit.Test;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RReadWriteLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;

import java.util.concurrent.TimeUnit;

public class RunRedisson {
    public static void main(String[] args) throws InterruptedException {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://172.16.26.30:6379")
                .setPassword("fcku098123A")
                .setDatabase(12)

        ;
        RedissonClient redissonClient = Redisson.create(config);

        String key = System.currentTimeMillis() + "";


        RLock rLock1 = redissonClient.getLock(key);

        //加锁 不指定时间才可以启用 监听狗 则默认用 30s; 每当过 1/3时间=10s 延迟时间 到30s
        rLock1.lock();
        System.err.println("获取到1");


        TimeUnit.SECONDS.sleep(60);


        //必须解锁否则这个key一直存在
        System.err.println("解锁");
        rLock1.unlock();
    }


    @Test
    public void t2() {
        Config config = new Config();
        config.useSingleServer()
                .setAddress("redis://127.0.0.1:6379")
//                .setPassword("fcku098123A")
                .setDatabase(1)

        ;
        RedissonClient redissonClient = Redisson.create(config);
        RReadWriteLock readWriteLock = redissonClient.getReadWriteLock("L");
//        RLock rLock = readWriteLock.readLock();
//        rLock.lock();
//        rLock.lock();
//        readWriteLock.readLock().unlock();
//        readWriteLock.readLock().unlock();
        readWriteLock.writeLock().lock();
        readWriteLock.writeLock().unlock();


    }
}
